/**   
* @Package com.app.open.sys.permission.web 
*/
package com.wlyy.bcwlw.sys.permission.web;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.commons.lang.StringUtils;
import org.directwebremoting.annotations.RemoteProxy;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;

import com.wlyy.bcwlw.base.BaseController;
import com.wlyy.bcwlw.common.tree.TreeNode;
import com.wlyy.bcwlw.sys.permission.dao.PermissionDAO;
import com.wlyy.bcwlw.sys.permission.entity.PermissionDTO;
import com.wlyy.bcwlw.sys.permission.service.PermissionService;
import com.wlyy.bcwlw.sys.role.dao.RoleDAO;
import com.wlyy.bcwlw.sys.role.entity.RoleDTO;
import com.wlyy.bcwlw.sys.user.dao.UserDAO;
import com.wlyy.bcwlw.sys.user.entity.UserDTO;

/**
 * @ClassName. SysPermissionController
 * @Description. TODO
 * @author zhangyang
 * @date 2015年8月10日 下午5:23:40
 * @version V1.0
 */
@RemoteProxy
@Controller
@RequestMapping("/sys/permission")
public class SysPermissionController extends BaseController {
	@Autowired
	private PermissionService permissionService;
	@Autowired
	private PermissionDAO permissionDAO;
	@Autowired
	private RoleDAO roleDAO;
	@Autowired
	private UserDAO userDAO;
	
	
	@RequestMapping("/permissionTree")
	public String permissionTreeView(HttpServletRequest request, HttpServletResponse response){
		return "sys/permission/permission-tree";
	}
	
	@RequestMapping("/getRoleList")
	public String getRolesByPermissionId(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int curPage = getPageNum(request);
		int limit = getInternal(request);
		String permissionId = get(request,"permissionId");
		
		List<RoleDTO> roleList = roleDAO.queryRoleInfoByPermissionId(permissionId);
		
		processJsonData4Grid(request, response, limit, curPage, roleList.size(), roleList);
		
		return null;
	}
	
	@RequestMapping("/getUserList")
	public String getUsersByPermissionId(HttpServletRequest request, HttpServletResponse response) throws Exception{
		int curPage = getPageNum(request);
		int limit = getInternal(request);
		String permissionId = get(request,"permissionId");
		
		List<UserDTO> userList = userDAO.queryUserByPermissionId(permissionId);
		
		processJsonData4Grid(request, response, limit, curPage, userList.size(), userList);
		return "sys/permission/permission-tree";
	}
	
	@RequestMapping("/checkIsNameExist")
	public String checkIsNameExist(HttpServletRequest request, HttpServletResponse response) throws Exception{
		String name = get(request,"name");
		String moduleId = get(request,"moduleId");
		String id = get(request,"id");
		
		PermissionDTO permissionDTO = new PermissionDTO();
		permissionDTO.setPermissionModuleId(moduleId);
		permissionDTO.setName(name);
		int count = permissionDAO.countPermissionByNameWithModuleId(permissionDTO);
		String str ="true";
		if(count >0){
			str="false";
		}
		if(StringUtils.isNotEmpty(id)){
			permissionDTO = permissionDAO.findById(id);
			if(permissionDTO!=null&&permissionDTO.getName().equals(name)){
				str="true";
			}
		}
		sendMessage(response, str);
		return null;
	}
	
	public PermissionDTO getPermissionInfoById(String permissionId){
		if(StringUtils.isEmpty(permissionId)){
			return null;
		}
		PermissionDTO permissionDTO = permissionDAO.findPermissionInfoById(permissionId);
		return permissionDTO;
	}
	
	
	public TreeNode getAllPermissionTreeById(){
		return permissionService.getAllPermissionTreeById();
	}
	
	
	
	public  PermissionDTO savePermissionInfo(PermissionDTO permissionDTO,String moduleId){
		return permissionService.savePermissionInfo(permissionDTO,moduleId);
	}
	
	public boolean removePermissionById(String permissionId){
		return permissionService.removePermissionById(permissionId);
	}
	
	public Map<String,Object> showPermissionOwnMenuTree(String permissionId){
		return permissionService.showPermissionOwnMenuTree(permissionId);
	}
	
	
	public boolean savePermissionOwnMenu(String permissionId,String[] ownMenus){
		return permissionService.savePermissionOwnMenu(permissionId, ownMenus);
	}
	

}
